package com.Hwadee.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.Hwadee.commonutils.R;
import com.Hwadee.educms.entity.CrmBanner;
import com.Hwadee.educms.entity.vo.BannerVo;
import com.Hwadee.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台管理 前端控制器
 * </p>
 *
 * @author 677
 * @since 2021-07-08
 */
@RestController
@RequestMapping("/educms/banneradmin")
//@CrossOrigin//跨域
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    //1.分页查询banner
    @GetMapping("/pageBanner/{current}/{limit}")
    public R pageBanner(@PathVariable("current") long current,
                        @PathVariable("limit") long limit) {
        Page<CrmBanner> page = new Page<>(current, limit);
        bannerService.page(page, null);
        return R.ok().data("items", page.getRecords()).data("total", page.getTotal());
    }

    //2.添加banner
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody BannerVo bannerVo) {
        bannerService.saveBanner(bannerVo);
        return R.ok();
    }

    //3.修改banner
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody BannerVo bannerVo) {
        bannerService.updateBanner(bannerVo);
        return R.ok();
    }

    //4.删除banner
    @DeleteMapping("/remove/{id}")
    public R removeBanner(@PathVariable("id") String id){
        bannerService.removeById(id);
        return R.ok();
    }

    //5.根据id获取banner
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("items", banner);
    }

}

